#include "LightRenderer.h"

namespace GL
{

    LightRenderer::LightRenderer(ObjectData &data, const std::string &shaderSource)
    {

        Awake(data);
        ReadShaderSource(shaderSource);

        CreateVertexBuffer(vertexs);

        const int vertexShader = CreateVertexShader(VertexSource);
        const int fragmentShader = CreateFragmentShader(FragmentSource);

        Program = CreateProgram(vertexShader, fragmentShader);

        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0);
        glEnableVertexAttribArray(0);

        glBindBuffer(GL_ARRAY_BUFFER, 0);
        glBindVertexArray(0);

        GL_ERROR("{0}", glGetError());
    }

    LightRenderer::~LightRenderer()
    {
    }

    void LightRenderer::Awake(ObjectData &data)
    {

        float copyVertexs[108] = {
            // 无纹理
            data.l, data.b, data.f,
            data.r, data.b, data.f,
            data.r, data.t, data.f,
            data.r, data.t, data.f,
            data.l, data.t, data.f,
            data.l, data.b, data.f,
            data.l, data.b, data.n,
            data.r, data.b, data.n,
            data.r, data.t, data.n,
            data.r, data.t, data.n,
            data.l, data.t, data.n,
            data.l, data.b, data.n,
            data.l, data.t, data.n,
            data.l, data.t, data.f,
            data.l, data.b, data.f,
            data.l, data.b, data.f,
            data.l, data.b, data.n,
            data.l, data.t, data.n,
            data.r, data.t, data.n,
            data.r, data.t, data.f,
            data.r, data.b, data.f,
            data.r, data.b, data.f,
            data.r, data.b, data.n,
            data.r, data.t, data.n,
            data.l, data.b, data.f,
            data.r, data.b, data.f,
            data.r, data.b, data.n,
            data.r, data.b, data.n,
            data.l, data.b, data.n,
            data.l, data.b, data.f,
            data.l, data.t, data.f,
            data.r, data.t, data.f,
            data.r, data.t, data.n,
            data.r, data.t, data.n,
            data.l, data.t, data.n,
            data.l, data.t, data.f};

        for (int i = 0; i < 108; i++)
        {
            vertexs[i] = copyVertexs[i];
        }
    }

    void LightRenderer::Start()
    {
    }

    void LightRenderer::CreateIndexesBuffer(unsigned int indexes[])
    {
        unsigned int EBO;
        glGenBuffers(1, &EBO);

        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6 * sizeof(float), indexes, GL_STATIC_DRAW);
    }

    void LightRenderer::CreateVertexBuffer(float vertexs[])
    {

        unsigned int vertexBuffer;

        glGenBuffers(1, &vertexBuffer);
        // 顶点缓冲数组
        //  创建顶点缓冲
        glGenVertexArrays(1, &VertexArray);

        // 绑定顶点缓冲
        glBindVertexArray(VertexArray);

        glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
        glBufferData(GL_ARRAY_BUFFER, 108 * sizeof(float), vertexs, GL_STATIC_DRAW);
    }
    // Texture

    void LightRenderer::Render()
    {

        useProgram();
        SetMatrix();
        glDrawArrays(GL_TRIANGLES, 0, 36);

        // glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
    }

}